<template>
	<view>
		<u-sticky bgColor="#fff">
			<u-tabs :list="list" lineWidth="80" lineColor="#0F53AC" :activeStyle="activeStyle" :scrollable="false"
				:inactiveStyle="inactiveStyle" :itemStyle="itemStyle" @click="tabsClick" :current="current">
			</u-tabs>
		</u-sticky>
		<view class="tab-page">
			<view class="mySearch">
				<u-search placeholder="搜索" :clearabled="true" v-model="params.keyword" @search="chaxun" @change="chaxun" @custom="chaxun"
					height="40" shape="square" color="#2B2B2B" :showAction="false"></u-search>
			</view>
			<view class="myTags">
				{{current== 0 ? '仅展示近两月未开始的培训及已开始的全部培训' : '仅查看用户当年全部已完成的培训任务'}}
			</view>
			<view class="list-wrapper" v-for="(item,index) in wbList" :key="index" @click="handleToInfo(item)">
				<view class="list-info">
					<view class="list-title">
						<view class="title-left">
							{{item.name}}
						</view>
						<view class="title-right"
							:class="[current == 0?(item.trainState==1?'title-close':item.trainState==2?'title-dyz':'title-overdue'):'title-finish']">
							{{current == 0?(item.trainState==1?'未开始':item.trainState==2?'已开始':''):'已完成'}}
						</view>
					</view>
				</view>
				<view class="list-type">
					<view class="tag">
						{{item.categoryName}}
					</view>
				</view>
				<view class="list-main">
					<view class="item-main">
						<view class="main-left">
							<view class="txt1">培训方式</view>
							<view class="txt2">{{item.trainWay==1?'线上':'线下'}}</view>
						</view>
						<view class="main-left">
							<view class="txt1">考核方式</view>
							<view class="txt2">{{renderWay(item.assessWay)}}</view>
						</view>
					</view>
					<view class="item-main">
						<view class="main-left" style="width: 100%;">
							<view class="txt1">培训时间</view>
							<view class="txt2">{{item.trainStartTime}}至{{item.trainEndTime}}</view>
						</view>
					</view>
				</view>
			</view>
			<u-loadmore v-if="total>5" :status="status" :line="true" />
		</view>
		<view class="empty-data" v-if="wbList.length==0">
			<u-empty mode="data" :marginTop="100" text="暂无数据"></u-empty>
		</view>
		<view class="bottom-subside" style='height: 22px;'></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '',
				actionStyle: {
					color: '#fff',
					background: '#0F53AC',
					padding: '10rpx',
					borderRadius: '8rpx'
				},
				value: '',
				status: 'loadmore',
				activeStyle: {
					color: '#0F53AC',
					fontWeight: 'bold',
					transform: 'scale(1.05)'
				},
				inactiveStyle: {
					color: '#8c8c8c',
					transform: 'scale(1)'
				},
				itemStyle: {
					paddingLeft: '30rpx',
					paddingRight: '30rpx',
					paddingTop: '24rpx',
					paddingBottom: '24rpx',
					height: '48rpx'
				},
				current: 0,
				list: [{
					name: '未完成',
				}, {
					name: '已完成',
				}],

				wbList: [],
				params: {
					keyword: '', //关键字
					trainStatus: 0, //0.待培训  1.已培训
					pn: 1,
					ps: 5,
				},
				pages: 1,
				total: 0
			}
		},
		onShow() {
			this.wbList = [];
			this.params.pn = 1;
			this.getList();
		},
		// 下拉刷新
		onPullDownRefresh() {
			this.wbList = [];
			this.params.pn = 1;
			this.getList();
		},
		methods: {
			tabsClick(e) {
				this.current = e.index
				if (this.current == 0) { //待维保任务
					this.params.pn = 1;
					this.params.trainStatus = 0;
					this.wbList = [];
					this.getList();
				}
				if (this.current == 1) { //已维保任务
					this.wbList = [];
					this.params.pn = 1;
					this.params.trainStatus = 1;
					this.getList();
				}

			},
			getList() {
				uni.$u.http.post(`/train/mobile/train/trainList`, this.params).then(res => {
					if (res.code == 0) {
						this.wbList = this.wbList.concat(res.data.records);
						this.pages = res.data.pages;
						this.total = res.data.total;
						uni.stopPullDownRefresh()
					}
				}).catch(err => {})
			},
			chaxun() {
				this.wbList = []
				this.params.pn = 1
				this.getList()
			},
			handleToInfo(item) {
				if (this.current == 0) {
					uni.navigateTo({
						url: `/pages/subPackagesC/mineTrain/trainInfo?id=${item.id}&trainState=${item.trainState}`
					})

				} else {
					// 已完成
					uni.navigateTo({
						url: `/pages/subPackagesC/mineTrain/trainFinish?id=${item.id}`
					})
				}

				// 未完成 - 查看详情
				// uni.navigateTo({
				// 	url: '/pages/subPackagesC/mineTrain/trainInfo' 
				// })
				// 未完成 - 去记录
				// uni.navigateTo({
				// 	url: '/pages/subPackagesC/mineTrain/trainSuccess' 
				// })
				// 已完成
				// uni.navigateTo({
				// 	url: '/pages/subPackagesC/mineTrain/trainFinish'
				// })
			},
			renderWay(data) {
				const typeForm = [{
					value: 1,
					label: '笔试'
				}, {
					value: 2,
					label: '实操'
				}]
				const newArr = []
				data.split(',').map(item => {

					if (typeForm.filter(it => it.value == item).length) {
						newArr.push(typeForm.filter(it => it.value == item)[0].label)
					}
				})
				return newArr.join(',')
			}
		},
		onReachBottom() {
			if (this.params.pn >= this.pages) return;
			this.status = 'loading';
			this.params.pn = ++this.params.pn;
			setTimeout(() => {
				this.getList()
				if (this.params.pn >= this.pages) this.status = 'nomore';
				else this.status = 'loading';
			}, 1000)
		}
	}
</script>

<style lang="scss" scoped>
	page {
		background: #ffffff;
	}

	.tab-page {
		border-top: 1px solid #f2f2f2;

		.mySearch {
			background-color: #fff;
			padding:28rpx 32rpx;

			.u-input {
				background-color: #f2f2f2;
			}
		}
		
		.myTags{
			font-size: 14px;
			font-weight: 400;
			// line-height: 42rpx;
			text-align: center;
			background-color: #F5F5F5;
			color: #8C8C8C;
			padding: 28rpx 0;
		}

		margin-top: 2rpx;
	}

	.item-title {
		font-size: 24rpx;
		color: #8c8c8c;
		padding: 20rpx 30rpx 10rpx 30rpx;
	}

	.dropdown {
		.list2 {
			.sort {
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 26rpx;
				color: #000000;
				padding: 20rpx 0;
			}
		}

		.sort-list {
			width: 100%;
			height: 100vh;
			background: #9d9d9d;
			z-index: 22;
			position: absolute;

			.sort-item {
				display: flex;
				justify-content: space-between;
				align-items: center;
				padding: 10rpx 40rpx 10rpx 20rpx;
				font-size: 34rpx;
				border-bottom: 1px solid #f2f2f2;
				z-index: 99;
				position: relative;
				background: #fff;
			}

			.sort-item:last-child {
				border-bottom: none;
			}

			.sort-item:first-child {
				border-top: 1px solid #f2f2f2;
			}
		}
	}

	.list-wrapper {
		.list-type {
			display: flex;
			padding: 0 32rpx ;
			.tag{
				text-align: center;
				color: rgba(102, 102, 102, 1);
				background: rgba(229, 229, 229, 1);
				font-size: 24rpx;
				line-height: 17.38px;
				// max-width: 120rpx;
				border-radius: 8rpx;
				padding:8rpx 24rpx;
				overflow: hidden; //超出的文本隐藏
				text-overflow: ellipsis; //溢出用省略号显示
				white-space: nowrap; // 默认不换行；
			}
		}

		.list-info {
			margin-top: 2rpx;
			background: #fff;
			padding: 40rpx 32rpx 10rpx 32rpx;

			.list-title {
				display: flex;
				align-items: center;
				justify-content: space-between;

				.title-left {
					width: 70%;
					display: flex;
					align-items: center;
					font-size: 17px;
					font-weight: 500;
					color: #2B2B2B;
					word-break: break-all;
					word-wrap: break-word;
					overflow: hidden;
					text-overflow: ellipsis; //溢出用省略号显示
					// white-space: nowrap; // 默认不换行；

					.copy {
						color: #8c8c8c;
						font-size: 20rpx;
						margin-left: 10rpx;
						border-radius: 8rpx;
						background: #f2f2f2;
						padding: 5rpx 10rpx;
					}
				}

				.title-right {
					min-width: 74rpx;
					height: 48rpx;
					line-height: 48rpx;
					text-align: center;
					border-radius: 8rpx;
					font-size: 24rpx;
					padding: 0 20rpx;
				}

				.title-ongoing {
					background-color: rgba(15, 83, 172, 0.2);
					color: #0F53AC;
				}

				.title-dyz {
					background-color: rgba(15, 83, 172, 0.2);
					color: #0F53AC;
				}

				.title-close {
					background-color:  rgba(252, 119, 10, 0.2);
					color: #FC770A;
				}

				.title-finish {
					background-color: rgba(2, 173, 130, 0.2);
					color: #02AD82;
				}

				.title-overdue {
					background-color:  rgba(255, 77, 77, 0.2);
					color: #FF4D4D;
				}
			}

		}

		.list-main {
			padding: 4rpx 10rpx 40rpx 10rpx;
			background: #fff;
			border-bottom: 1px solid #f2f2f2;

			.item-main {
				display: flex;
				justify-content: space-between;
				padding: 4rpx 30rpx;

				.main-left {
					width: 50%;
					text-align: left;

					.txt1 {
						color: #8C8C8C;
						font-size: 15px;
						font-weight: 400;
						padding: 10rpx 0;
					}

					.txt2 {
						width: 100%;
						color: #4D4D4D;
						font-size: 16px;
						font-weight: 400;
					}

					.txt-zc {
						color: #0F53AC;
					}

					.txt-yc {
						color: #fc770a;
					}
				}
			}

			.gz-desc {
				padding: 15rpx 30rpx;

				.title {
					font-size: 28rpx;
					color: #8c8c8c;
				}

				.desc-reason {
					font-size: 28rpx;
					color: #2b2b2b;
					margin-top: 15rpx;
					overflow: hidden;
					text-overflow: ellipsis;
					display: -webkit-box;
					-webkit-line-clamp: 2;
					-webkit-box-orient: vertical;
				}
			}
		}

		.item-btn {
			display: flex;
			justify-content: space-around;
			align-items: center;
			padding: 20rpx 0;
			background: #fff;

			.chuku {
				width: 45%;
				font-size: 34rpx;
				color: #8c8c8c;
				text-align: center;
			}

			.ruku {
				width: 45%;
				font-size: 34rpx;
				color: #0F53AC;
				text-align: center;
			}

			.su-line {
				width: 4rpx;
				height: 50rpx;
				background: #f2f2f2;
			}
		}
	}
</style>
